package RandomName;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class MyJFrame extends JFrame {
    JPanel jp1;


    public MyJFrame() throws HeadlessException {
        init();
        initCenter();
        initSouth();
        this.setVisible(true);
    }

    private void initSouth() {
        JPanel jp2 = new JPanel();
        JButton jButton = new JButton("点名");

        jButton.addActionListener(new ActionListener() {
            Thread thread;
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jButton.getText().equals("点名")) {
                    for (int i = 0; i < jp1.getComponents().length; i++) {
                        jp1.getComponent(i).setBackground(Color.white);
                    }
                    thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Random r = new Random();
                            while (true) {
                                Component component = jp1.getComponent(r.nextInt(jp1.getComponentCount()));
                                component.setBackground(Color.red);
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                component.setBackground(Color.white);
                            }
                        }
                    });
                    jButton.setText("停止");

                    thread.start();
                } else {
                    jButton.setText("点名");
                    thread.stop();
                }
            }
        });
        jp2.add(jButton);
        this.add(jp2, BorderLayout.SOUTH);
    }

    private void initCenter() {
        jp1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        try {
            InputStreamReader reader=new InputStreamReader(MyJFrame.class.getClassLoader().getResourceAsStream("names.text"),"UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String s;
            while (true){
                    if (!((s=br.readLine())!=null)) break;
                JButton jb = new JButton(s);
                jb.setBackground(Color.white);
                jp1.add(jb);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        this.add(jp1, BorderLayout.CENTER);
    }

    private void init() {
        this.setSize(600, 400);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.setTitle("点名");
    }
}
